<template>
  <div class="designer-container">
    <!-- 顶部三个工具图标 -->
    <div class="top-tools">
      <div class="tool-button">
        <el-icon><Back /></el-icon>
      </div>
      <div class="tool-button">
        <el-icon><Right /></el-icon>
      </div>
      <div class="tool-button">
        <el-icon><Collection /></el-icon>
      </div>
    </div>
    
    <!-- 主设计区 -->
    <div class="canvas-container">
      <!-- 顶部标尺 - 红框部分 -->
      <div class="horizontal-ruler">
        <div class="ruler-markers">
          <div v-for="i in 16" :key="i" class="ruler-marker">{{ (i-1) * 125 }}</div>
        </div>
      </div>
      
      <!-- 左侧标尺 - 红框部分 -->
      <div class="vertical-ruler">
        <div class="ruler-markers">
          <div v-for="i in 6" :key="i" class="ruler-marker">{{ (i-1) * 250 }}</div>
        </div>
      </div>
      
      <!-- 中间画布 -->
      <div class="canvas-area">
        <canvas ref="fabricCanvas" class="design-canvas"></canvas>
      </div>
      
      <!-- 底部控制栏 - 红框部分 -->
      <div class="bottom-controls">
        <!-- 左侧页面指示器 -->
        <div class="page-indicator">
          <el-icon><Picture /></el-icon>
          <span>1/1</span>
          <el-icon><ArrowDown /></el-icon>
        </div>
        
        <!-- 中间底部标签栏 -->
        <div class="tabs-bar">
          <div class="tab" :class="{ active: activeTab === 'template' }" @click="activeTab = 'template'">
            <el-icon><Files /></el-icon>
            <span>模版</span>
          </div>
          <div class="tab" :class="{ active: activeTab === 'text' }" @click="activeTab = 'text'">
            <el-icon><EditPen /></el-icon>
            <span>文字</span>
          </div>
          <div class="tab" :class="{ active: activeTab === 'image' }" @click="activeTab = 'image'">
            <el-icon><Picture /></el-icon>
            <span>图片</span>
          </div>
        </div>
        
        <!-- 右侧缩放工具 -->
        <div class="zoom-tools">
          <el-button size="small" circle>
            <el-icon><ZoomIn /></el-icon>
          </el-button>
          <el-button size="small" circle>
            <el-icon><ZoomOut /></el-icon>
          </el-button>
          <el-button size="small" circle>
            <el-icon><FullScreen /></el-icon>
          </el-button>
          <el-button size="small" circle>
            <el-icon><View /></el-icon>
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { Canvas, Pattern } from 'fabric';
import { 
  Back, Right, Collection, EditPen, Picture, Files, 
  ZoomIn, ZoomOut, FullScreen, View, ArrowDown
} from '@element-plus/icons-vue';

// 画布引用
const fabricCanvas = ref(null);
let canvas = null;

// 当前选中的标签
const activeTab = ref('template');

// 初始化画布
onMounted(() => {
  canvas = new Canvas(fabricCanvas.value, {
    width: 800,
    height: 600,
    backgroundColor: '#ffffff'
  });
  
  // 创建棋盘背景
  createCheckerboardBackground();
});

onUnmounted(() => {
  if (canvas) {
    canvas.dispose();
  }
});

// 创建棋盘背景
function createCheckerboardBackground() {
  const patternCanvas = document.createElement('canvas');
  const patternContext = patternCanvas.getContext('2d');
  
  patternCanvas.width = 20;
  patternCanvas.height = 20;
  
  patternContext.fillStyle = '#f0f0f0';
  patternContext.fillRect(0, 0, 10, 10);
  patternContext.fillRect(10, 10, 10, 10);
  
  patternContext.fillStyle = '#e0e0e0';
  patternContext.fillRect(10, 0, 10, 10);
  patternContext.fillRect(0, 10, 10, 10);
  
  const backgroundPattern = new Pattern({
    source: patternCanvas,
    repeat: 'repeat'
  });
  
  canvas.setBackgroundColor(backgroundPattern, canvas.renderAll.bind(canvas));
}
</script>

<style scoped>
.designer-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

/* 顶部工具栏 */
.top-tools {
  display: flex;
  height: 40px;
  background-color: #fff;
  border-bottom: 1px solid #e0e0e0;
  padding: 0 10px;
}

.tool-button {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #555;
  font-size: 20px;
}

.tool-button:hover {
  background-color: #f5f5f5;
}

/* 画布容器 */
.canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background-color: #f0f0f0;
}

/* 水平标尺 - 顶部红框部分 */
.horizontal-ruler {
  position: absolute;
  top: 0;
  left: 40px;
  right: 0;
  height: 30px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #e0e0e0;
  z-index: 10;
  display: flex;
  align-items: flex-end;
}

.horizontal-ruler .ruler-markers {
  display: flex;
  width: 100%;
  height: 100%;
  padding-bottom: 3px;
}

.horizontal-ruler .ruler-marker {
  flex: 1;
  text-align: center;
  font-size: 10px;
  color: #999;
  position: relative;
}

.horizontal-ruler .ruler-marker::before {
  content: '';
  position: absolute;
  height: 5px;
  width: 1px;
  background-color: #ccc;
  bottom: 12px;
  left: 50%;
}

/* 垂直标尺 - 左侧红框部分 */
.vertical-ruler {
  position: absolute;
  top: 30px;
  left: 0;
  bottom: 50px;
  width: 40px;
  background-color: #f5f5f5;
  border-right: 1px solid #e0e0e0;
  z-index: 10;
  overflow: hidden;
}

.vertical-ruler .ruler-markers {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

.vertical-ruler .ruler-marker {
  height: 16.66%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 5px;
  font-size: 10px;
  color: #999;
  position: relative;
}

.vertical-ruler .ruler-marker::before {
  content: '';
  position: absolute;
  height: 1px;
  width: 5px;
  background-color: #ccc;
  right: 20px;
  top: 50%;
}

/* 画布区域 */
.canvas-area {
  position: absolute;
  top: 30px;
  left: 40px;
  right: 0;
  bottom: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.design-canvas {
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

/* 底部控制栏 - 底部红框部分 */
.bottom-controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 50px;
  background-color: #fff;
  border-top: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  z-index: 20;
}

/* 页面指示器 */
.page-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
  color: #666;
  background: #fff;
  padding: 5px 10px;
  border-radius: 4px;
  border: 1px solid #e0e0e0;
  cursor: pointer;
}

/* 底部标签栏 - 主要实现部分 */
.tabs-bar {
  display: flex;
  height: 100%;
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 80px;
  font-size: 12px;
  color: #666;
  cursor: pointer;
  transition: all 0.2s;
}

.tab:hover {
  background-color: #f5f5f5;
}

.tab.active {
  color: #007bff;
  border-top: 2px solid #007bff;
  background-color: #f5f5f5;
}

.tab .el-icon {
  font-size: 18px;
  margin-bottom: 2px;
}

/* 右侧缩放工具 */
.zoom-tools {
  display: flex;
  gap: 5px;
}
</style>